﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using PsychveyApp1.classes;

namespace PsychveyApp1
{
    public partial class ViewAllSurvey : System.Web.UI.Page
    {
        static UserAccount ua = new UserAccount();
        static Survey sv1 = new Survey();

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PsychAppDBConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            lblAdminUsername.Text = (string)(Session["username"]);

            BindData();
        }

        private void BindData()
        {
            string strQuery = "select * from survey";
            SqlCommand cmd = new SqlCommand(strQuery);
            dgvSurveyDetails.DataSource = DBManager.GetAllSurveys();
            dgvSurveyDetails.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            
            Response.Redirect("ViewAllUser.aspx"); 
        }

 

        protected void dgvSurveyDetails_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = dgvSurveyDetails.SelectedRow.RowIndex;
            dgvSurveyDetails.SelectedIndex.ToString();
            GridViewRow row = dgvSurveyDetails.SelectedRow;

            sv1 = new Survey();
            sv1.title = row.Cells[0].Text;
            sv1.dateCreated = row.Cells[1].Text;
            sv1.dateModified = row.Cells[2].Text;
            sv1.userID = row.Cells[3].Text;
            sv1.typeOfSurvey = row.Cells[4].Text;
            sv1.surveyID = row.Cells[5].Text;
            Session["currentSurvey"] = sv1;
            Session["currentPageNumber"] = 1;

            Answer ans = new Answer();
            ans.surveyID = sv1.surveyID;
            ans.userID = sv1.userID;
           
            DBManager.DeleteAnswersBySurveyID(sv1.surveyID);
            DBManager.DeleteQuestionsBySurveyID(sv1.surveyID);
            DBManager.DeleteUserAnswersBySurveyID(sv1.surveyID);
            DBManager.DeleteSurvey(sv1);
            Response.Redirect("ViewAllSurvey.aspx");
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminHome.aspx");
        }

        
    }
}